package com.agile.major.service.impl;

import com.agile.major.domain.BaseStudentInfo;
import com.agile.major.repository.BaseStudentInfoRepository;
import com.agile.major.service.BaseStudentInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;


/**
 * Service Implementation for managing BaseStudentInfo.
 */
@Service
@Transactional
public class BaseStudentInfoServiceImpl implements BaseStudentInfoService{

    private final Logger log = LoggerFactory.getLogger(BaseStudentInfoServiceImpl.class);

    private final BaseStudentInfoRepository baseStudentInfoRepository;

    public BaseStudentInfoServiceImpl(BaseStudentInfoRepository baseStudentInfoRepository) {
        this.baseStudentInfoRepository = baseStudentInfoRepository;
    }
    @Autowired
    JdbcTemplate jdbcTemplate;
    /**
     * Save a baseStudentInfo.
     *
     * @param baseStudentInfo the entity to save
     * @return the persisted entity
     */
    @Override
    public BaseStudentInfo save(BaseStudentInfo baseStudentInfo) {
        log.debug("Request to save BaseStudentInfo : {}", baseStudentInfo);
        return baseStudentInfoRepository.save(baseStudentInfo);
    }

    /**
     *  Get all the baseStudentInfos.
     *
     *  @param pageable the pagination information
     *  @return the list of entities
     */
    @Override
    @Transactional(readOnly = true)
    public Page<BaseStudentInfo> findAll(Pageable pageable) {
        log.debug("Request to get all BaseStudentInfos");
        return baseStudentInfoRepository.findAll(pageable);
    }

    /**
     *  Get one baseStudentInfo by id.
     *
     *  @param id the id of the entity
     *  @return the entity
     */
    @Override
    @Transactional(readOnly = true)
    public BaseStudentInfo findOne(Long id) {
        log.debug("Request to get BaseStudentInfo : {}", id);
        return baseStudentInfoRepository.findOne(id);
    }

    /**
     *  Delete the  baseStudentInfo by id.
     *
     *  @param id the id of the entity
     */
    @Override
    public void delete(Long id) {
        log.debug("Request to delete BaseStudentInfo : {}", id);
        baseStudentInfoRepository.delete(id);
    }

    @Override
    public Map getStuCount(Long baseMajorId, String overYear) {
        String sql = "SELECT COUNT(stu_no) stu_number FROM `base_student_info` a WHERE a.`base_major_id` = "+baseMajorId+" AND a.over_year='"+overYear+"'";
        return jdbcTemplate.queryForMap(sql);
    }
}
